﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.Common;
using System.Transactions;
using Apps.Models.Flow;
using Apps.IBLL.Flow;
using Apps.IDAL.Flow;
using Apps.BLL.Core;
using Apps.Locale;
using System.Linq.Expressions;

namespace Apps.BLL.Flow
{
    public partial class Flow_FormBLL
    {

        [Dependency]
        public IFlow_TypeRepository typeRep { get; set; }

        public List<Flow_FormModel> GetListByTypeId(string typeId)
        {
            IQueryable<Flow_Form> queryData = m_Rep.GetList(a => a.TypeId == typeId);
            return CreateModelList(ref queryData);

        }
        /// <summary>
        /// 流程类型名称模糊匹配
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Flow_FormModel> GetListByName(string name)
        {
            IQueryable<Flow_Form> queryData = m_Rep.GetList(a => a.Flow_Type.Name.Contains(name));
            return CreateModelList(ref queryData);
        }


        public void Delete(Expression<Func<Flow_Form, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                foreach (var entity in db.Flow_Form.Where(whereLambda))
                {
                    db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                }
                db.SaveChanges();
            }
        }
 

        public Flow_FormModel GetByExternalURL(string externalurl)
        { 
            Flow_Form entity = m_Rep.GetItem(p => p.ExternalURL.Contains(externalurl));
            if (entity == null) return null;
            Flow_FormModel model = new Flow_FormModel();
            model.Id = entity.Id;
            model.Name = entity.Name;
            model.Remark = entity.Remark;
            model.UsingDep = entity.UsingDep;
            model.TypeId = entity.TypeId;
            model.State = entity.State;
            model.CreateTime = entity.CreateTime;
            model.HtmlForm = entity.HtmlForm;
            model.IsExternal = entity.IsExternal;
            model.ExternalURL = entity.ExternalURL;
            model.AttrA = entity.AttrA;
            model.AttrB = entity.AttrB;
            model.AttrC = entity.AttrC;
            model.AttrD = entity.AttrD;
            model.AttrE = entity.AttrE;
            model.AttrF = entity.AttrF;
            model.AttrG = entity.AttrG;
            model.AttrH = entity.AttrH;
            model.AttrI = entity.AttrI;
            model.AttrJ = entity.AttrJ;
            model.AttrK = entity.AttrK;
            model.AttrL = entity.AttrL;
            model.AttrM = entity.AttrM;
            model.AttrN = entity.AttrN;
            model.AttrO = entity.AttrO;
            model.AttrP = entity.AttrP;
            model.AttrQ = entity.AttrQ;
            model.AttrR = entity.AttrR;
            model.AttrS = entity.AttrS;
            model.AttrT = entity.AttrT;
            model.AttrU = entity.AttrU;
            model.AttrV = entity.AttrV;
            model.AttrW = entity.AttrW;
            model.AttrX = entity.AttrX;
            model.AttrY = entity.AttrY;
            model.AttrZ = entity.AttrZ;

            return model;

        }
         
        public override List<Flow_FormModel> CreateModelList(ref IQueryable<Flow_Form> queryData)
        {

            List<Flow_FormModel> modelList = (from r in queryData
                                              select new Flow_FormModel
                                              {
                                                  Id = r.Id,
                                                  Name = r.Name,
                                                  Remark = r.Remark,
                                                  UsingDep = r.UsingDep,
                                                  TypeId = r.TypeId,
                                                  TypeName = r.Flow_Type.Name,
                                                  State = r.State,
                                                  CreateTime = r.CreateTime,
                                                  AttrA = r.AttrA,
                                                  IsExternal = r.IsExternal,
                                                  ExternalURL = r.ExternalURL
                                              }).ToList();

            return modelList;
        }
    }
}
